//
//  XMVisitorView.m
//  sinaWeiBo
//
//  Created by 孟发 on 4/7/16.
//  Copyright © 2016 夏孟发. All rights reserved.
//

#import "XMVisitorView.h"
#import "Masonry.h"
#import "UIButton+XMExtension.h"
#import "UIImageView+XMExtension.h"
#import "UILabel+XMExtension.h"

@interface XMVisitorView ()

/** 底层轮播图片框 */
@property (nonatomic, strong)UIImageView *iconView;
/** 遮罩图像 */
@property (nonatomic,strong)UIImageView *maskIconView;

/** 小房子 */
@property (nonatomic,strong)UIImageView *homeIconView;
/** 消息文字 */
@property (nonatomic,strong)UILabel *messageLabel;

/** 注册按钮 */
@property (nonatomic,strong)UIButton *registerButton;
/** 登录按钮 */
@property (nonatomic,strong)UIButton *loginButton;

@property (nonatomic, strong)UIButton *button;



@end

@implementation XMVisitorView

#pragma mark 懒加载

- (UIImageView *)iconView{
//    if (_iconView == nil) {
//        _iconView = [[UIImageView alloc] init];
//        _iconView.image = [UIImage imageNamed:@"visitordiscover_feed_image_smallicon"];
//        [self sizeToFit];
//    }
//    return _iconView;
    if (_iconView == nil) {
        _iconView = [UIImageView setupImageViewWith:@"visitordiscover_feed_image_smallicon"];
    }
    return _iconView;
}
- (UIImageView *)maskIconView{
//    if (_maskIconView == nil) {
//        _maskIconView = [[UIImageView alloc] init];
//        _maskIconView.image = [UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"];
//        [self sizeToFit];
//    }
    if (_maskIconView == nil) {
        _maskIconView = [UIImageView setupImageViewWith:@"visitordiscover_feed_mask_smallicon"];
    }
    return _maskIconView;
}
- (UIImageView *)homeIconView{
//    if (_homeIconView == nil) {
//        _homeIconView = [[UIImageView alloc] init];
//        _homeIconView.image = [UIImage imageNamed:@"visitordiscover_feed_image_house"];
//        [self sizeToFit];
//    }
    if (_homeIconView == nil) {
        _homeIconView = [UIImageView setupImageViewWith:@"visitordiscover_feed_image_house"];
    }
    
    return _homeIconView;
}
- (UILabel *)messageLabel{
//    if (_messageLabel == nil) {
//        _messageLabel = [[UILabel alloc] init];
//        _messageLabel.text = @"关注一些人，回这里看看有什么惊喜";
//        _messageLabel.textColor = [UIColor darkGrayColor];
//        _messageLabel.font = [UIFont systemFontOfSize:14];
//        _messageLabel.numberOfLines = 0;
//        _messageLabel.textAlignment = NSTextAlignmentCenter;
////        _messageLabel.bounds.size.width = 224.0;
////        [self sizeToFit];
//    }
    if (_messageLabel == nil) {
        _messageLabel = [UILabel setupLabel:@"关注一些人，回这里看看有什么惊喜"];
    }
    return _messageLabel;
}


/** 注册按钮 */
- (UIButton *)registerButton{
//    if (_registerButton == nil) {
//        _registerButton = [[UIButton alloc] init];
//        [_registerButton setTitle:@"注册" forState:UIControlStateNormal];
//        [_registerButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//        [_registerButton setBackgroundColor:[UIColor whiteColor]];
//        [self sizeToFit];
//        self 
//     }
//    return _registerButton;
    if (_registerButton == nil) {
        _registerButton = [UIButton setupButton:@"注册" color:[UIColor orangeColor] imageName:@"common_button_white_disable"];
        [self sizeToFit];
    }
    
    return _registerButton;
}
/** 登录按钮 */
- (UIButton *)loginButton{
//    if (_loginButton == nil) {
//        _loginButton = [[UIButton alloc] init];
//        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
//        [_loginButton setBackgroundColor:[UIColor whiteColor]];
//        [_loginButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//        [self sizeToFit];
//    }
//    return _loginButton;
    if (_loginButton == nil) {
        _loginButton = [UIButton setupButton:@"登录" color:[UIColor whiteColor] imageName:@"common_button_white_disable"];
    [_loginButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [self sizeToFit];

    }
    
    return _loginButton;

}




- (instancetype)initWithFrame:(CGRect)frame{
   self = [super initWithFrame:frame];
    [self setupUI];
    return self;
}

- (void)setupUI{
    /** 添加控件 */
    [self addSubview:self.iconView];
    [self addSubview:self.maskIconView];
    [self addSubview:self.homeIconView];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registerButton];
    [self addSubview:self.loginButton];
    

    /**
     *  控件布局
     */
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        
    }];
    [self.maskIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        
    }];
    [self.homeIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView).with.offset(50);
        make.centerX.mas_equalTo(self.iconView.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(280, 70));

    }];
    
    /** 按钮 */
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.messageLabel.mas_left).with.offset(0);
        make.bottom.equalTo(self.messageLabel).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(110, 30));
    }];
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.registerButton.mas_top);
        make.right.equalTo(self.messageLabel.mas_right).with.offset(0);
        make.size.mas_equalTo(CGSizeMake(110, 30));

    }];
    
    /**
     *  设置背景颜色
     */
    self.backgroundColor = [UIColor colorWithWhite:237.0/255.0 alpha:1.0];
}


#pragma mark 开启首页转轮动画

- (void)startAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.toValue = @(2 * M_PI);
    animation.repeatCount = MAXFLOAT;
    animation.duration = 20;
    //用在不断重复的动画上,当动画绑定的图层对应的视图被销毁时,动画会自动被销毁
    animation.removedOnCompletion = false;
    //添加打图层
    [self.iconView.layer addAnimation:animation forKey:nil];
}

/**
 *  设置视图信息
 */
- (void)setupInfo:(NSString *)imageName title:(NSString *)title{
    self.messageLabel.text = title;
    //如果图片名称为nil,说明是首页(因为首页直接继承基类即可,无需再传参数),直接返回
    if (imageName ==nil) {
        //播放动画
        [self startAnimation];
        return;
    }else{
        //换图片
        self.iconView.image = [UIImage imageNamed:imageName];
        //隐藏小房子
        self.homeIconView.hidden = YES;
        //将遮罩图像隐藏
        self.maskIconView.hidden = YES;
    }
}
@end
